import Vue from 'vue'
import Vuex from 'vuex'
import student from './student'
Vue.use(Vuex)

export default new Vuex.Store({
  // 存放组件需要共享数据
  state: {
    numA:100,
    persons:[
      {name:'zhangsan',age:23},
      {name:'xuyuan',age:21},
    ]
  },
  getters: {
    fmtNumA(state){
      return "***//***" + state.numA + "元";
    }
  },
  mutations: {
    addPerson(state,payload){
      console.log("payload",payload);
      state.persons.push(payload)
      // 这里不要写异步代码
    }
  },
  // 方法：类似于事件，与mutations类似
  // 区别：可以包含异步操作，它提交到mutations，不直接修改数据。
  actions: {
    // let {commot} = {state:{},commit:fn} 对象解构
    // let {a,b} = {a:100,b:200,c:300}
    asyncAddPerson({ commit },payload) {
      console.log("actions",payload);
      // 异步方式添加用户 setTimeout模拟 == axios请求
      setTimeout(() => {
        // 提交到(调用)mutions进行添加
        context.commit('addPerson',payload)
      },3000)
    }
  },
  // 定义模块
  modules: {
    student
  }
})
